<?php
include "Class/EasyMySQL.php";

$mysql = new EasyMySQL();
if(!$mysql->Connect())  return;

$method = $_REQUEST["Method"];
$data = file_get_contents('php://input', 'r');
$data = iconv("GB2312", "UTF-8//IGNORE", $data);

if($method == "Select")
{
    $Names =  "Device.UID AS UID,Device.Name AS Name,Device.DeviceTypeUID AS DeviceTypeUID,DeviceType.Name AS DeviceTypeName,
                Device.OfficeUID AS OfficeUID,Office.Name AS OfficeName,
                OfficeUID AS OfficeUID,Office.Name AS OfficeName,
                City.UID AS CityUID,City.Name AS CityName,
                Province.UID AS ProvinceUID,Province.Name AS ProvinceName,
                Device.Address AS Address,
                GROUP_CONCAT(DeviceTime.Time) AS Time";
    $mysql->SELECT("Device", $Names)
        ->LEFTJOIN("DeviceType", "Device.DeviceTypeUID=DeviceType.UID")
        ->LEFTJOIN("Office", "Device.OfficeUID=Office.UID")
        ->LEFTJOIN("City", "Office.CityUID=City.UID")
        ->LEFTJOIN("Province", "City.ProvinceUID=Province.UID")
        ->LEFTJOIN("DeviceTime", "Device.UID=DeviceTime.DeviceUID")
        ->WhereSelect("OfficeUID=".$data)
        ->GroupBy("Device.UID")
        ->QuerySelect();
}
else if($method == "Insert")
{
    $mysql->Insert("Device")->InsertColumns("Name,DeviceTypeUID,OfficeUID,Address")->InsertValues($data, 4)->QueryInsert();
}
else if($method == "Update")
{
    $array = explode("#", $data);
    $mysql->Update("Device")->UpdateSet($array[0])->WhereUpdate("UID=".$array[1])->Query();
}
else if($method == "Delete")
{
    $mysql->Delete("Device")->WhereDelete("UID", $data)->Query();
}

$mysql->Close();
?>